#include "unp.h"

#define fifo1 "/tmp/fifo1"
#define fifo2 "/tmp/fifo2"

void client(int,int) , server(int,int);

int main(int argc ,char *argv[])
{
    int in,out;
    pid_t childpid;
   
    if((mkfifo(fifo1,FILE_MODE)) ==-1 && errno != EEXIST) {
       err_sys("mkfifo errror");
    }

    if((mkfifo(fifo2,FILE_MODE)) == -1 && errno != EEXIST) {
       unlink(fifo1);
       err_sys("mkfifo errror");
    }



    if((childpid=fork()) ==-1)
    {
       err_sys("fork error");
    }else if(childpid == 0){ //child
       //in = open(fifo1,O_RDONLY,0);
       out = open(fifo2,O_WRONLY,0);
       server(in,out);
       exit(0);
    }

    out = open(fifo1,O_WRONLY,0); //must be first for child process has opened by read
    in = open(fifo2,O_RDONLY,0);
     
    client(in,out);

    int stat;
    while((waitpid(childpid,&stat,0)) == -1){
        if(errno != EINTR){
          stat=-1;
          break;
        }
    }

    unlink(fifo1);
    unlink(fifo2);

    return (stat);
}
